package com.baizhi.controller;

import com.baizhi.dto.PageDTO;
import com.baizhi.dto.TwoCategoryPageDTO;
import com.baizhi.entity.Category;
import com.baizhi.service.CategoryService;
import com.baizhi.vo.CommonMessageVO;
import com.baizhi.vo.CommonVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("category")
@CrossOrigin
public class CategoryController {

    @Resource
    private CategoryService categoryService;
    private static final Logger log = LoggerFactory.getLogger(CategoryController.class);


    @PostMapping("queryOnePage")
    public CommonVO queryOnePage(@RequestBody PageDTO pageDTO){
        log.info("pageDTO {}",pageDTO);
        CommonVO commonVO = categoryService.queryOnePage(pageDTO);
        return commonVO;
    }
    /**
     * 添加一级类别
     */
    @PostMapping("add")
    public CommonMessageVO add(@RequestBody Category category){
        log.info("Category {}",category);
        CommonMessageVO add = categoryService.add(category);
        return add;
    }

    /**
     * 删除一级类别
     */
    @PostMapping("delete")
    public CommonMessageVO delete(@RequestBody Category category){
        log.info("category: {}",category);
        CommonMessageVO delete = categoryService.delete(category);
        return delete;
    }

    /**
     * 获取二级类别
     */
    @PostMapping("queryTwoPage")
    public CommonVO queryTwoPage(@RequestBody TwoCategoryPageDTO twoCategoryPageDTO) {
        log.info("twoCategoryPageDTO {}",twoCategoryPageDTO);
        CommonVO commonVO = categoryService.queryTwoCategory(twoCategoryPageDTO);
        return commonVO;
    }
    /**
     * 修改类别名
     */
    @PostMapping("update")
    public CommonMessageVO update(@RequestBody Category category){
        log.info("category: {}",category);
        CommonMessageVO update = categoryService.update(category);
        return update;
    }

    /**
     * 根据id查
     */
    @GetMapping("queryById")
    public Category queryById(String id){
        log.info("id: {}",id);
        //根据id查
        Category category = categoryService.queryById(id);
        return category;
    }

    /**
     * 将一级数据传输到添加页面
     */
    @PostMapping("queryByLevelsCategory")
    public List<Category> queryByLevelsCategory(Category category){
        log.info("category {}",category);
        List<Category> categories = categoryService.queryAllLevels(category);
        return categories;
    }

}
